package com.lx.work;

import java.io.*;
import java.util.Scanner;

/**
 * @author c先生
 * @date 2022年04月06日 上午 11:53:29
 * @describe     作业：使用字符流完成注册登录功能  要求 登录只能登录三次
 */

public class Work1 {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入注册用户名：");
        String userName = sc.next();
        System.out.println("请输入注册密码：");
        String psw = sc.next();

        reg(userName,psw);
        int count = 0;
        while (count<3){
        System.out.println("请输入登录用户名：");
        String userNamel = sc.next();
        System.out.println("请输入登录密码：");
        String pswl = sc.next();
            if (login(userNamel, pswl)) {
                System.out.println("登录成功！");
                break;
            } else {
                count++;
                System.out.println("登录失败！");
                continue;
            }
        }
    }

    private static void reg(String userName,String psw) throws IOException {
        if(userName.contains("-") || psw.contains("-")){
            System.out.println("用户名或者密码包含非法字符“-”");
            return;
        }
        BufferedWriter bw = new BufferedWriter(new FileWriter("E:/Users/c先生/Desktop/uesr.txt"));
        String userNameAndPsw = userName+"-"+psw;
        bw.write(userNameAndPsw);
        bw.close();
    }
    private static boolean login(String userName, String psw) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("E:/Users/c先生/Desktop/uesr.txt"));
        String[] split = br.readLine().split("-");
        if(split[0].equals(userName) && split[1].equals(psw) ){
            return true;
        }
        return false;
    }
}
